package com.rxw.nutmall;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.rxw.nutmall.service.BaseService;
import com.rxw.nutmall.service.impl.BaseServiceImpl;

import java.util.Collections;

public class ProductGenerator {
    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");
        String moduleName = "/nut-product";

        AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);

        //全局配置
        generator.global(globalConfig(projectPath,moduleName));
        //包配置
        generator.packageInfo(packageConfig(projectPath, moduleName));
        //策略配置
        generator.strategy(strategyConfig());

        generator.execute(new FreemarkerTemplateEngine());
    }
    private static final DataSourceConfig DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://192.168.31.100:3306/nutmall_pms?serverTimezone=Asia/Shanghai","root","rena061498")
            .schema("nutmall_pms")
            .build();


    private static GlobalConfig globalConfig(String projectPath, String moduleName) {
        return new GlobalConfig.Builder()
                .outputDir(projectPath+moduleName+"/src/main/java")
                .author("咸蛋超人")
                .enableSwagger()
                .dateType(DateType.ONLY_DATE)
                .commentDate("yyyy-MM-dd")
                .build();
    }

    private static PackageConfig packageConfig(String projectPath, String moduleName) {
        return new PackageConfig.Builder()
                .parent("com.rxw.nutmall")
                .moduleName("product")
                .entity("entity")
                .service("service")
                .controller("controller")
                .mapper("mapper")
                .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath+moduleName+"/src/main/resources/mapper"))
                .build();
    }

    private static StrategyConfig strategyConfig() {
        return new StrategyConfig.Builder()
                .addTablePrefix("pms_")
                .entityBuilder()
                .enableTableFieldAnnotation()
                .idType(IdType.ASSIGN_ID)
                .enableLombok()
                .serviceBuilder()
                .formatServiceFileName("%sService")
                .superServiceClass(BaseService.class)
                .superServiceImplClass(BaseServiceImpl.class)
                .controllerBuilder()
                .enableRestStyle()
                .mapperBuilder()
                .enableMapperAnnotation()
                .build();
    }
}
